﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Pyramid.Bll;
using Pyramid.Models;
using System.Data.Entity;

namespace Pyramid.Controllers
{
	public class AdminCenterController : PaymentBaseController
	{
		private readonly AdminCenterHelper _helper = new AdminCenterHelper();
		private readonly SelectListHelper _dropDownListHelper = SelectListHelper.NewInstance;

		#region admin center base info

		public ActionResult List(AdminCenterQueryModel model)
		{
			return View(PrepareForListView(model));
		}

		public ActionResult SelectList(AdminCenterQueryModel model)
		{
			return View(PrepareForListView(model));
		}

		private List<AdminCenter> PrepareForListView(AdminCenterQueryModel model)
		{
			var centers = _helper.GetList(model, User);

			var currentPage = (model.PageNum ?? 1) - 1;
			var pageSize = model.NumPerPage ?? 20;
			var vm = new PagerModel
			{
				PageSize = pageSize,
				CurrentPageIndex = currentPage,
				TotalCount = centers.Count()
			};
			ViewBag.Bag = vm;
			ViewBag.QueryModel = model;
			ViewBag.MarketSelectList = _dropDownListHelper.MarketZones(null, true);
			return centers.OrderBy(x => x.Id).Skip(currentPage*pageSize).Take(pageSize).ToList();
		}

		public ActionResult Create()
		{
			var model = new AdminCenter
				{
					EstablishDate = DateTime.Parse("1982-01-01"),
					CreateUserId = User.UserId,
					CreateTime = DateTime.Now,
					EditUserId = User.UserId,
					EditTime = DateTime.Now
				};
			ViewBag.MarketSelectList = _dropDownListHelper.MarketZones();
			return View("Edit", model);
		}

		public ActionResult Edit(int id)
		{
			var model = Db.AdminCenters.First(x => x.Id == id);
			ViewBag.MarketSelectList = _dropDownListHelper.MarketZones(model.ZoneId, true);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(AdminCenter model)
		{
			if (Db.AdminCenters.Any(x => x.Id != model.Id && x.Code == model.Code))
			{
				return JError("编码已被占用，请修改！");
			}
			if (model.Id == 0)
			{
				model.CreateUserId = User.UserId;
				model.CreateTime = DateTime.Now;
				model.EditUserId = User.UserId;
				model.EditTime = DateTime.Now;
				Db.AdminCenters.Add(model);
			}
			else
			{
				model.EditUserId = User.UserId;
				model.EditTime = DateTime.Now;
				Db.Entry(model).State = EntityState.Modified;
			}
			Db.SaveChanges();

			return JSuccess("操作成功！", "tab_acm_archive");
		}

		public ActionResult Delete(int id)
		{
			var model = new AdminCenter {Id = id};
			Db.Entry(model).State = EntityState.Deleted;
			Db.SaveChanges();
			return JSuccess("删除成功！", "tab_acm_archive");
		}

		#endregion

		#region admin center account

		public ActionResult AccountList(AdminCenterQueryModel model)
		{
			return View(PrepareForListView(model));
		}

		public ActionResult SetAccount(int adminCenterId)
		{
			var center = _helper.GetAdminCenter(adminCenterId);
			ViewBag.Account = string.IsNullOrWhiteSpace(center.UserId) ?
								new Account {Id = 0, ExtInfoId = center.Id, ExtInfoName = center.Name, UserType = (int) UserTypes.AdminCenter, FullName = center.Name}
								: Db.Accounts.Include(x => x.Roles).First(x => x.UserId == center.UserId);

			return View(center);
		}

		[HttpPost]
		public ActionResult SetAccount(int adminCenterId, Account model, int[] selectedRoleIds)
		{
			var helper = AccountHelper.NewInstance;
			return helper.InsertOrUpdate4AdminCenter(adminCenterId, model, selectedRoleIds) ? JSuccess("账号设置成功！", "tab_acm_acaccount") : JError(helper.Message);
		}

		#endregion

		#region admin center finance

		public ActionResult PaymentList(PaymentQueryModel model)
		{
			model.PayerType = (int) PayerTypes.AdminCenter;
			return View(PrepareForPaymentListView(model));
		}

		public ActionResult CreatePaymentApply()
		{
			var record = new PayeeRecord {ApplyTime = DateTime.Now, ApplyUserId = User.UserId, ApplyUserName = User.FullName, PayerType = (int) PayerTypes.AdminCenter};
			ViewBag.FinanceProjects = _dropDownListHelper.FinanceProjects(PayerTypes.AdminCenter);
			return View("EditPaymentApply", record);
		}

		public ActionResult EditPaymentApply(int id)
		{
			var record = PaymentHelper.NewInstance.Single(id);
			ViewBag.FinanceProjects = _dropDownListHelper.FinanceProjects(PayerTypes.AdminCenter);
			return View(record);
		}

		[HttpPost]
		public ActionResult EditPaymentApply(PayeeRecord model)
		{
			model.PayerType = (int) PayerTypes.AdminCenter;

			PaymentHelper.NewInstance.InsertOrUpdate(model, User);
			return JSuccess("操作成功！", "tab_acm_payment");
		}

		public ActionResult DeletePaymentApply(int id)
		{
			var helper = PaymentHelper.NewInstance;
			return helper.Delete(id) ? JSuccess("删除成功！", "tab_acm_payment") : JError(helper.Message);
		}

		#endregion
	}
}